﻿using Cart.SanPhamSrv;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cart
{
    public partial class PopupThemSP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["IdLoaiSanPham"] != null)
                {
                    long idsanpham = (Request.QueryString["sanpham"] == "" ? 0 : Convert.ToInt64(Request.QueryString["sanpham"]));
                    //long idsanpham = 1;
                    if (idsanpham != 0)
                    {
                        using (var sanphamService = new SanPhamSrv.SanPhamServiceClient())
                        {
                            var modelsanpham = sanphamService.ChiTietSanPham(idsanpham);
                            hdfIdSanPham.Value = idsanpham.ToString();
                            imgAnh.ImageUrl = "a_files/" + modelsanpham.Anh;

                            txtTenSanPham.Text = modelsanpham.Ten;
                            txtSoLuong.Text = modelsanpham.SoLuong.ToString();
                            txtGia.Text = modelsanpham.Gia.ToString();
                        }
                    }
                    else
                    {
                        hdfIdSanPham.Value = "0";



                    }
                    long IdLSP = Convert.ToInt64(Session["IdLoaiSanPham"]);
                    using (var spService = new SanPhamSrv.SanPhamServiceClient())
                    {
                        var listLSP = spService.DanhSachLoaiSanPham();
                        if (listLSP != null)
                        {
                            foreach (var ls in listLSP)
                            {
                                if (ls.Id == IdLSP) lbLoai.InnerText = ls.Ten.ToUpper();
                            }
                        }
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('Không lấy được dữ liệu sản phẩm');</script>");
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.close();", true);
                }
            }
        }

        protected void register_button1_Click(object sender, EventArgs e)
        {
            using (var spService = new SanPhamSrv.SanPhamServiceClient())
            {

               
                string result = "";
                if (hdfIdSanPham.Value != "0")
                {
                    result = "Cập nhật ";
                }
                else result = "Thêm mới ";

                if (Session["IdLoaiSanPham"] != null)
                {
                    if (fuAnh.HasFile)
                    {
                        var sp = new SanPham();
                        if (hdfIdSanPham.Value != "0")
                        {
                            sp.Id = Convert.ToInt64(hdfIdSanPham.Value);
                        }
                        sp.Ten = txtTenSanPham.Text;
                        sp.Anh = fuAnh.FileName;
                        sp.SoLuong = Convert.ToInt64(txtSoLuong.Text);
                        sp.Gia = Convert.ToInt64(txtGia.Text);
                        sp.IdLoai = Convert.ToInt64(Session["IdLoaiSanPham"]);
                        string theFileName = Path.Combine(Server.MapPath("~/a_files"), fuAnh.FileName);
                        if (File.Exists(theFileName))
                        {
                            File.Delete(theFileName);
                        }
                        fuAnh.SaveAs(theFileName);
                        sp.Anh = fuAnh.FileName;
                        result += spService.CapNhatSanPham(sp);
                    }
                    else
                    {
                        if (hdfIdSanPham.Value != "0")
                        {
                            var sp = new SanPham();
                            sp.Id = Convert.ToInt64(hdfIdSanPham.Value);
                            sp.Ten = txtTenSanPham.Text;
                            sp.SoLuong = Convert.ToInt64(txtSoLuong.Text);
                            sp.Gia = Convert.ToInt64(txtGia.Text);
                            sp.Anh = null;
                            result += spService.CapNhatSanPham(sp);
                        }
                    }
                }
                
                
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + result + "');</script>"); 
                Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.close();if (window.opener && !window.opener.closed) {window.opener.location.reload();}", true);
            }
        }
        protected void UploadThisFile(FileUpload upload)
        {
            if (upload.HasFile)
            {
                string theFileName = Path.Combine(Server.MapPath("~/a_files"), upload.FileName);
                if (File.Exists(theFileName))
                {
                    File.Delete(theFileName);
                }
                upload.SaveAs(theFileName);
            }
        }
    }
}